package util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PageUtils {
    
    public PageUtils() {
        super();
    }

    /**
     * <p>int[0] = valid begin, int[1] = valid end, int[2] = validated parameter begin</p>
     * <code>int i = int[0]; i < int[1]; i++</code>
     * @param total 总数
     * @param begin 开始数
     * @param size 单页数
     * @return int[3]
     */
    public static int[] getExtent(int total, int begin, int size) {
        int[] extent = new int[3];
        
        if (0 < total && 0 < size) {
            int quotient = total / size;
            int remainder1 = total % size;
            int remainder2 = begin % size;
            int integer = begin - remainder2;
            
            int last_begin = (0 < remainder1) ? size * quotient : size * (quotient - 1);
            int _begin = 0 > integer ? 0 : (last_begin < integer ? last_begin : integer);
            int _end = (total < _begin + size ? total : (_begin + size));
            
            extent[0] = _begin;
            extent[1] = _end;
            extent[2] = 0 > begin ? 0 : (total <= begin ? total - 1 : begin);
        }
        
        return extent;
    }
    

    public static List<Object[]> getPages(int total, int begin, int size) {
        return getPages(total, begin, size, 4, "上一页", "下一页");
    }
    
    public static List<Object[]> getPages(int total, int begin, int size, int more, String previous, String next) {
        List<Object[]> pages = new ArrayList<Object[]>(0);
        
        if (0 < total && 0 < size) {
            pages.add(new Object[]{0 <= begin - size ? begin - size : 0, previous});
            for (int i = more; i >= 1; i--) {
                int b = begin - i * size;
                if (0 <= b) {
                    pages.add(new Object[]{b, b / size + 1});
                }
            }
            pages.add(new Object[]{begin, begin / size + 1});
            for (int i = 1; i <= more; i++) {
                int b = begin + i * size;
                if (total > b) {
                    pages.add(new Object[]{b, b / size + 1});
                }
            }
            pages.add(new Object[]{total > begin + size ? begin + size : begin, next});
        }
        
        return pages;
    }
    
    public static void main(String[] args) {
        System.out.println(Arrays.toString(getExtent(56, 5, 10)));
    }
    
}
